﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SpeedTest
{
	public partial class HistoryForm : Form
	{
		private string itemID = String.Empty;
		private string envID = String.Empty;
		private Data data = null;
		private DataTable resultsTable = null;

		public HistoryForm(Data data, string itemID, string envID)
		{
			InitializeComponent();
			this.data = data;
			this.itemID = itemID;
			this.envID = envID;
		}

		private void HistoryForm_Load(object sender, EventArgs e)
		{
			if (data != null)
			{
				resultsTable = data.GetResultsByCaseID(itemID, envID);
				if (resultsTable != null && resultsTable.Rows.Count > 0)
				{
					DisplayResults(resultsTable);
					gridHistory.Rows[0].Selected = true;
				}
			}
		}

		private void DisplayResults(DataTable table)
		{
			foreach (DataRow tableRow in table.Rows)
			{
				string[] row = new string[gridHistory.Columns.Count];
				row[0] = tableRow["Result"].ToString();
				row[1] = tableRow["ReferenceNumber"].ToString();
				row[2] = tableRow["Name"].ToString();
				row[3] = tableRow["ExecutionDate"].ToString();
				row[4] = tableRow["Active"].ToString();

				gridHistory.Rows.Add(row);
			}
		}

		private void gridHistory_SelectionChanged(object sender, EventArgs e)
		{
			if (gridHistory.SelectedRows.Count > 0)
			{
				txtNotes.Text = resultsTable.Rows[gridHistory.SelectedRows[0].Index]["Notes"].ToString();
			}
			else
			{
				txtNotes.Text = String.Empty;
			}
		}

	}
}
